﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LPV_POS.Module;
using System.Resources;
using System.Globalization;

namespace LPV_POS.mh_pos.mh_ban_hang
{
    public partial class frmDM_GHU_CHU_BEP : Form
    {
        CultureInfo culture;
        string sghichu = "";
        string sMAHH = "";
        object[] o;
        string[] s;
        thuvien tv;
        DataTable tb;

        #region "Muti language"

        private void SetLanguage_banhang(string cultureName)
        {
            try
            {
                culture = CultureInfo.CreateSpecificCulture(cultureName);
                ResourceManager rm = new ResourceManager("LPV_POS.Muti_language.MyResource", typeof(mh_banhang6_PhuThu).Assembly);
                lbel_tencongty.Text = rm.GetString("ghichu", culture);
                btnThem.Text = rm.GetString("them", culture);
                btnSua.Text = rm.GetString("sua", culture);
                btnXoa.Text = rm.GetString("xoa", culture);
                bton_thuchien_1.Text = rm.GetString("dongy", culture);
                bton_thoat_1.Text = rm.GetString("huy", culture);
                chkTatCa.Text = rm.GetString("hienthitatca", culture);
                 
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Note");
            }

        }

        #endregion

        public frmDM_GHU_CHU_BEP(string _sghichu, string sTenMon, string _sMAHH)
        {
            InitializeComponent();
            sghichu = _sghichu;
            sMAHH = _sMAHH;
            if (sTenMon != "")
                lbel_tencongty.Text = lbel_tencongty.Text + "(" + sTenMon + ")";
        }
        private int f_addnote(string ghichu)
        {
            int ketqua;
            tv = new thuvien();
            s = new string[] { "@flag", "@OBJNAME", "@MAHH" };
            o = new object[] { 1, ghichu, sMAHH };
            ketqua = tv.SP_Update("sp_DM_GHU_CHU_BEP", s, o);
            return ketqua;
        }
        private DataTable f_selectnote()
        {
       
            tv = new thuvien();
            if (chkTatCa.Checked)
            {
                s = new string[] { "@flag" };
                o = new object[] { 5 };
            }
            else
            {
                s = new string[] { "@flag", "@MAHH" };
                o = new object[] { 5,sMAHH };
            }
            tb = tv.SP_SelectDataTable("sp_DM_GHU_CHU_BEP", s, o);
            DataView dv_temp = tb.DefaultView;
            dv_temp.Sort = "OBJID DESC";
            return dv_temp.ToTable();
        }

        private int f_editnote(int maghichu, string ghichu)
        {
            int ketqua;
            tv = new thuvien();
            s = new string[] { "@flag", "@OBJID", "@OBJNAME", "@MAHH" };
            o = new object[] { 2, maghichu, ghichu ,sMAHH};
            ketqua = tv.SP_Update("sp_DM_GHU_CHU_BEP", s, o);
            return ketqua;

        }

        private int f_deletenote(int maghichu)
        {
            int ketqua;
            tv = new thuvien();
            s = new string[] { "@flag", "@OBJID"};
            o = new object[] { 3, maghichu };
            ketqua = tv.SP_Update("sp_DM_GHU_CHU_BEP", s, o);
            return ketqua;

        }

        private void f_dataoncard()
        {
            grdDM_GHU_CHU_BEP.MainView = card_ghichu;
            // chieu rong của moi card
            card_ghichu.CardWidth = 210;
            // khoang cach giua hai card
            card_ghichu.CardInterval = 5;
            // so cot lớn nhat của card layout
            card_ghichu.MaximumCardColumns = 4;
            card_ghichu.MaximumCardRows = -1;
            //card_ghichu.CardOptionsBehavior.FieldAutoHeight.
            card_ghichu.OptionsBehavior.FieldAutoHeight = true;
            //CardView.CalcFieldHeight
            card_ghichu.VertScrollVisibility = DevExpress.XtraGrid.Views.Base.ScrollVisibility.Auto;
            grdDM_GHU_CHU_BEP.DataSource = f_selectnote();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {

        }

        private void bton_thuchien_1_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }

        private void bton_thoat_1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void bton_chu_Click(object sender, EventArgs e)
        {

        }

        public string ShowForm()
        {
            this.ShowDialog();
            return txt_ghichu.Text.Trim();
        }

        private void frmDM_GHU_CHU_BEP_Load(object sender, EventArgs e)
        {
            cpublic ocpublic = new cpublic();
            string str_lang = ocpublic.Registry_Get("mh_main_lang");
            if (str_lang == "")
                str_lang = "vi-VN";
            SetLanguage_banhang(str_lang);


            Boolean chk = false;
            Boolean.TryParse(new cpublic().Registry_Get(this.Name + chkTatCa.Name) + "", out chk);
            chkTatCa.Checked = chk;

            //this.ShowData();
            this.f_dataoncard();
            txt_ghichu.Text = card_ghichu.GetRowCellValue(card_ghichu.FocusedRowHandle, col_tenghichu) + ""; 
        }

        void ShowData()
        {
            thuvien othuvien = new thuvien();
            DataTable tbDM_GHU_CHU_BEP = othuvien.SP_SelectDataTable("sp_DM_GHU_CHU_BEP",
                                                                    new string[] { "@flag" },
                                                                    new object[] { 4 });
            grdDM_GHU_CHU_BEP.DataSource = tbDM_GHU_CHU_BEP;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            int ketqua;
            Mod_messagebox mess = new Mod_messagebox();
            string str_ghichu  = new cpublic().loadformchu_point(300, 300);
            if (str_ghichu != "")
            {
                ketqua = f_addnote(str_ghichu);
                if (ketqua == -1)
                    mess.messagebox_pos("Thông báo", "Thêm không thành công", loaithongbao.thongtin.GetHashCode());
                else
                {
                    mess.messagebox_pos("Thông báo", "Thêm thành công", loaithongbao.thongtin.GetHashCode());
                    f_dataoncard();
                    txt_ghichu.Text = str_ghichu.Trim();

                }
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (card_ghichu.RowCount <= 0)
                return;
            int ketqua;
            int  maghichu = int.Parse(card_ghichu.GetRowCellValue(card_ghichu.FocusedRowHandle, col_maghichu) + "");
            string Tenghichu = card_ghichu.GetRowCellValue(card_ghichu.FocusedRowHandle, col_tenghichu) + "";
            Mod_messagebox mess = new Mod_messagebox();
            string str_ghichu = new cpublic().loadformchu_point(Tenghichu,300, 300);

            if (str_ghichu != "")
            {
                ketqua = f_editnote(maghichu, str_ghichu);
                if (ketqua == -1)
                    mess.messagebox_pos("Thông báo", "Sửa không thành công", loaithongbao.thongtin.GetHashCode());
                else
                {
                    mess.messagebox_pos("Thông báo", "Sửa thành công", loaithongbao.thongtin.GetHashCode());
                    f_dataoncard();
                    txt_ghichu.Text = str_ghichu.Trim();
                }
            }
            
            return;
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (card_ghichu.RowCount <= 0)
                return;
            if (MessageBox_LPV.Show("Bạn muốn xóa", MessageBox_LPV.chon.OkCancel) == DialogResult.OK)
            {
                int ketqua;
                Mod_messagebox mess = new Mod_messagebox();
                int maghichu = int.Parse(card_ghichu.GetRowCellValue(card_ghichu.FocusedRowHandle, col_maghichu) + "");
                ketqua = f_deletenote(maghichu);
                if (ketqua == -1)
                    mess.messagebox_pos("Thông báo", "Xóa không thành công", loaithongbao.thongtin.GetHashCode());
                else
                {
                    mess.messagebox_pos("Thông báo", "Xóa thành công", loaithongbao.thongtin.GetHashCode());
                    f_dataoncard();
                }
            }
        }



        private void gvDM_GHU_CHU_BEP_Click(object sender, EventArgs e)
        {
            txt_ghichu.Text = gvDM_GHU_CHU_BEP.GetFocusedValue() + "";
        }

        private void btnChon_Click(object sender, EventArgs e)
        {
            mh_chu omh_chu = new mh_chu();
            string str = omh_chu.ShowForm("Chi chú", txt_ghichu.Text, 300, 300, false);
            if (omh_chu.DialogResult == DialogResult.OK)
                txt_ghichu.Text = str;
        }

        private void card_ghichu_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            grdDM_GHU_CHU_BEP.MainView = card_ghichu;
            if (card_ghichu.RowCount > 0)
            {
                txt_ghichu.Text  = card_ghichu.GetRowCellValue(card_ghichu.FocusedRowHandle, col_tenghichu) + ""; 
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            string ghichu  = new cpublic().loadformchu_point("", 300, 300);
            if (ghichu != "")
            {
                txt_ghichu.Text = ghichu;
            }
        }

        private void chkTatCa_CheckedChanged(object sender, EventArgs e)
        {
            grdDM_GHU_CHU_BEP.DataSource = f_selectnote();
            btnSua.Enabled = !chkTatCa.Checked;
            btnThem.Enabled = !chkTatCa.Checked;

            new cpublic().Registry_Set(this.Name + chkTatCa.Name, chkTatCa.Checked);
        }
    }
}
